package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.mapper.*;
import com.qf.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2020-09-03
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CartCourseMapper cartCourseMapper;
    @Autowired
    CourseTeacherMapper courseTeacherMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    CourseDetailsMapper courseDetailsMapper;
    @Override
    public void addCartById(Integer uid,Integer cid) {
        if(cartMapper.selectById(uid)==null){
            cartMapper.cartAdd(new Cart(uid));
            Cart cart = cartMapper.selectById(cid);
            cartCourseMapper.insert(new CartCourse(cart.getId(),cid));
            List<Integer> all = cartCourseMapper.getAll(cart.getId());
            BigDecimal bigDecimal = new BigDecimal(0);
            for (Integer integer : all) {
                Course course = courseMapper.selectById(integer);
                bigDecimal.add(course.getCurrentPrice());
            }
            Integer count = cartCourseMapper.count(cart.getId());
            Cart cart1 = new Cart(cart.getId(), uid, count, bigDecimal);
            cartMapper.cartUpdate(cart);
        }else{
        Cart cart = cartMapper.selectById(cid);
        cartCourseMapper.insert(new CartCourse(cart.getId(),cid));
        List<Integer> all = cartCourseMapper.getAll(cart.getId());
        BigDecimal bigDecimal = new BigDecimal(0);
        for (Integer integer : all) {
            Course course = courseMapper.selectById(integer);
            bigDecimal.add(course.getCurrentPrice());
        }
        Integer count = cartCourseMapper.count(cart.getId());
        Cart cart1 = new Cart(cart.getId(), uid, count, bigDecimal);
        cartMapper.cartUpdate(cart);}
    }


    public List<CourseShow> getAllCart(Integer uid) {
        Cart cart = cartMapper.selectById(uid);
        List<Integer> all = cartCourseMapper.getAll(cart.getId());
        List<CourseShow> list=new ArrayList<>();
        for (Integer integer : all) {
            Course course = courseMapper.selectById(integer);
            CourseDetails courseDetails = courseDetailsMapper.selectById(course.getDetailsId());
            Integer tid = courseTeacherMapper.getTid(course.getId());
            Teacher teacher = teacherMapper.selectById(tid);
            CourseShow courseShow = new CourseShow(course.getId(), course.getCurrentPrice(), course.getSeason(), courseDetails.getTitle(), courseDetails.getInfo(), courseDetails.getTime(), teacher.getImages());
            list.add(courseShow);
        }

        return list;
    }


}
